package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.CustomException;
import com.example.mapper.CategoryMapper;
import com.example.pojo.Category;
import com.example.pojo.Dish;
import com.example.pojo.Setmeal;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;


    public void remove(Long id){
        /* 查询该分类是否有关联菜品 */
        //构造查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        //统计符合条件的行
        int count1 = dishService.count(dishLambdaQueryWrapper);

        if (count1 > 0){
            //该分类关联了菜品，抛出业务异常
            throw new CustomException("该分类关联了菜品，不能删除");
        }

        /* 查询该分类是否有关联套餐 */
        //构造查询条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        //统计符合条件的行
        int count2 = setmealService.count(setmealLambdaQueryWrapper);

        if (count2 > 0){
            //该分类关联了套餐，抛出业务异常
            throw new CustomException("该分类关联了套餐，不能删除");
        }

        //正常删除
        super.removeById(id);
    }
}
